#ifndef _RIGID_SURFACE_H_
#define _RIGID_SURFACE_H_

#include "surftrack.h"
#include "broadphasegrid.h"
#include <vector>

using namespace std;
using namespace eltopo2d;

#define FAIL	1e8

typedef struct  
{
	int pt_i;
	int st_i;
	int e_i;
	double max_t;
	bool active;
	Vec2d vel;
} Crack2d;

typedef struct  
{
	int a_i;
	int b_i;
	double a_time;
	double b_time;
} PotentialCollision;

typedef vector<Vec2d> VecList;
typedef vector<Vec2ui> ConList;
typedef vector<Crack2d*> CrackList;
typedef vector<PotentialCollision> PotentialCollisionList;

class RigidSurface
{
public:
	RigidSurface(void);
	~RigidSurface(void);
	RigidSurface(SurfTrack* surf);

	void advect(CrackList* crack, double dt);	

	void crackDirectionEvolution();

	void crackCollisionProcessing();

	void crackCollisionPreprocessing();

	void crackCollisionDetection();

	Vec2d edgeCollisionDetection(Vec2d as, Vec2d ae, Vec2d bs, Vec2d be);

	bool checkInsideHalfPlane(Vec2d cut, Vec2d dir);

	void crackIntegration();

	void crackThickening(int pt_i, Vec2d direction);

	void crackGeneration();

	void crackCut(int pt_i, Vec2d direction);

	void crackMerge(int pt_i, Vec2d dir);

	SurfTrack* surf;
	CrackList* crack;
	PotentialCollisionList* pcs;
	double dt;
	double eps;
};

bool sortByTime(const PotentialCollision &lhs, const PotentialCollision &rhs);

bool sortByMTime(const Crack2d &lhs, const Crack2d &rhs);

#endif
